package com.yueke.gemini.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.TimeUnit;


public class RedisClient {
    private static Logger logger = LoggerFactory.getLogger(RedisClient.class);
    private RedisTemplate<Serializable, Object> redisTemplate;

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate
                .opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean expire(Serializable key,long timeout){
        if(redisTemplate.hasKey(key)){
            return redisTemplate.expire(key,timeout,TimeUnit.SECONDS);
        }
        return false;
    }

    public boolean expire(Serializable key,long timeout,TimeUnit tu){
        if(redisTemplate.hasKey(key)){
            return redisTemplate.expire(key,timeout,tu);
        }
        return false;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Set query_keys(final String key) {
        Set result = null;
        try {
            result = redisTemplate.keys(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List<Map<String, Object>> query_result(final String key) {
        List<Map<String, Object>> redis_result = new ArrayList<Map<String, Object>>();
        try {
            Set<String> result = query_keys(key);
            if (result != null) {
                if (result.size() > 0) {
                    for (String str : result) {
                        Map<String, Object> map = new HashMap<>();
                        Object result_temp = get(str);
                        map.put(str, result_temp);
                        redis_result.add(map);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return redis_result;
    }


    public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        System.out.println("Connection to server sucessfully");
     /*   jedis.set("C10041^18652083705", "Redis y");
        jedis.set("C10061^18652083705", "Redis f");
        jedis.set("C10011^18652083705", "Redis h");
        jedis.set("C10091^18652083705", "Redis k");*/

        System.out.println(jedis.get("C10061^18652083705"));
       /* Set<String> f = jedis.keys("C10041^*");
        for (String str : f) {
            System.out.println(str);
        }

        System.out.println(f);*/
    }
}
